<template>
  <div id="teachingSchedulePage">
    <div style="display: flex; justify-content: space-between">
      <!-- 搜索栏 -->
      <a-form layout="inline" :model="searchParam" @finish="doSearch">
        <a-form-item label="教师">
          <a-input
            v-model:value="searchParam.teacherName"
            placeholder="请输入教师姓名"
            allow-clear
          />
        </a-form-item>
        <a-form-item label="班级">
          <a-input v-model:value="searchParam.className" placeholder="请输入班级名称" allow-clear />
        </a-form-item>
        <a-form-item label="学科">
          <a-input v-model:value="searchParam.subject" placeholder="请输入学科" allow-clear />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit"> 搜索 </a-button>
        </a-form-item>
      </a-form>
      <a-button type="primary" size="large" @click="showAddModel">新增授课安排</a-button>
    </div>
    <div style="margin-bottom: 32px"></div>
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a-space>
            <a-button type="primary" @click="showUpdateModel(record.id)">修改</a-button>
            <a-button type="primary" danger @click="doDelete(record.id)">删除</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
    <a-modal v-model:open="addModel" title="添加授课安排" @ok="doAdd">
      <div style="height: 16px"></div>
      <a-form
        :model="formDataAdd"
        name="addTeachingSchedule"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
      >
        <a-form-item label="教师" name="teacherName">
          <a-select v-model:value="formDataAdd.teacherId" placeholder="请选择教师">
            <a-select-option v-for="item in teacherList" :key="item.id" :value="item.id">{{
              item.teacherName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="班级" name="className">
          <a-select v-model:value="formDataAdd.classId" placeholder="请选择班级">
            <a-select-option v-for="item in studentGroupList" :key="item.id" :value="item.id">{{
              item.className
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="课程" name="subject">
          <a-select v-model:value="formDataAdd.subject" placeholder="请选择课程">
            <a-select-option v-for="item in courseList" :key="item.id" :value="item.courseName">{{
              item.courseName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="星期" name="weekday">
          <a-select v-model:value="formDataAdd.weekday" placeholder="请选择星期">
            <a-select-option v-for="item in weekdayList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="上课地点" name="classroom">
          <a-input v-model:value="formDataAdd.classroom" />
        </a-form-item>
        <a-form-item label="节数" name="lessonPeriod">
          <a-select v-model:value="formDataAdd.lessonPeriod" placeholder="请选择节数">
            <a-select-option v-for="item in lessonPeriodList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="学期" name="term">
          <a-select v-model:value="formDataAdd.term" placeholder="请选择学期">
            <a-select-option v-for="item in termList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal v-model:open="updateModel" title="更新授课安排" @ok="doUpdate">
      <div style="height: 16px"></div>
      <a-form
        :model="formDataUpdate"
        name="addTeachingSchedule"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
      >
        <a-form-item label="教师" name="teacherName">
          <a-select v-model:value="formDataUpdate.teacherId">
            <a-select-option v-for="item in teacherList" :key="item.id" :value="item.id">{{
              item.teacherName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="班级" name="className">
          <a-select v-model:value="formDataUpdate.classId">
            <a-select-option v-for="item in studentGroupList" :key="item.id" :value="item.id">{{
              item.className
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="课程" name="subject">
          <a-select v-model:value="formDataUpdate.subject" placeholder="请选择课程">
            <a-select-option v-for="item in courseList" :key="item.id" :value="item.courseName">{{
              item.courseName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="星期" name="weekday">
          <a-select v-model:value="formDataUpdate.weekday">
            <a-select-option v-for="item in weekdayList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="上课地点" name="classroom">
          <a-input v-model:value="formDataUpdate.classroom" />
        </a-form-item>
        <a-form-item label="节数" name="lessonPeriod">
          <a-select v-model:value="formDataUpdate.lessonPeriod">
            <a-select-option v-for="item in lessonPeriodList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="学期" name="term">
          <a-select v-model:value="formDataUpdate.term">
            <a-select-option v-for="item in termList" :key="item" :value="item">{{
              item
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import {
  addTeachingScheduleUsingPost,
  deleteTeachingScheduleUsingPost,
  getTeachingScheduleByIdUsingGet,
  listTeachingScheduleByPageUsingPost,
  updateTeachingScheduleUsingPost,
} from '@/api/teachingScheduleController.ts'
import { listTeacherUsingPost } from '@/api/teacherController.ts'
import { listStudentGroupUsingPost } from '@/api/studentGroupController.ts'
import { listCourseUsingPost } from '@/api/courseController.ts'

const addModel = ref<boolean>(false)
const updateModel = ref<boolean>(false)

const columns = [
  {
    title: '教师',
    dataIndex: 'teacherName',
  },
  {
    title: '班级',
    dataIndex: 'className',
  },
  {
    title: '课程',
    dataIndex: 'subject',
  },
  {
    title: '星期',
    dataIndex: 'weekday',
  },
  {
    title: '上课地点',
    dataIndex: 'classroom',
  },
  {
    title: '节数',
    dataIndex: 'lessonPeriod',
  },
  {
    title: '学期',
    dataIndex: 'term',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 定义数据
const dataList = ref<API.TeachingSchedule[]>([])
const total = ref(0)

const weekdayList = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
const lessonPeriodList = ['一二节课', '三四节课', '五六节课', '七八节课']
const termList = ['2024-2025-第一学期', '2024-2025-第二学期']

// 搜索条件
const searchParam = reactive<API.TeachingScheduleQueryRequest>({
  current: 1,
  pageSize: 10,
  teacherName: '',
  className: '',
  subject: '',
  weekday: '',
  classroom: '',
  lessonPeriod: '',
  term: '',
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParam.current,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共${total}条`,
  }
})

const doTableChange = (page: any) => {
  searchParam.current = page.current
  searchParam.pageSize = page.pageSize
  fetchData()
}

// 获取数据
const fetchData = async () => {
  const res = await listTeachingScheduleByPageUsingPost({
    ...searchParam,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

const teacherList = ref<API.Teacher[]>([])
const fechTeacherList = async () => {
  const res = await listTeacherUsingPost({})
  if (res.data.code === 0 && res.data.data) {
    teacherList.value = res.data.data
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}
const studentGroupList = ref<API.StudentGroup[]>([])
const fechStudentGroupList = async () => {
  const res = await listStudentGroupUsingPost({})
  if (res.data.code === 0 && res.data.data) {
    studentGroupList.value = res.data.data
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}
const courseList = ref<API.Course[]>([])
const fechCourseList = async () => {
  const res = await listCourseUsingPost({})
  if (res.data.code === 0 && res.data.data) {
    courseList.value = res.data.data
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
  fechTeacherList()
  fechStudentGroupList()
  fechCourseList()
})

const doSearch = () => {
  // 重置页码
  searchParam.current = 1
  fetchData()
}

const doDelete = async (id: number) => {
  if (!id) {
    return
  }
  const res = await deleteTeachingScheduleUsingPost({ id: id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}

const formDataAdd = reactive<API.TeachingScheduleAddRequest>({
  teacherName: '',
  className: '',
  subject: '',
  weekday: '',
  classroom: '',
  lessonPeriod: '',
  term: '',
})
const formDataUpdate = ref<API.TeachingScheduleUpdateRequest>({
  teacherName: '',
  className: '',
  subject: '',
  weekday: '',
  classroom: '',
  lessonPeriod: '',
  term: '',
})

const doUpdate = async () => {
  const res = await updateTeachingScheduleUsingPost({
    ...formDataUpdate.value,
  })
  if (res.data.code === 0 && res.data.data !== null) {
    message.success('更新成功')
    fetchData()
    updateModel.value = false
  } else {
    message.error('更新失败, ' + res.data.message)
  }
}

const doAdd = async () => {
  const res = await addTeachingScheduleUsingPost({
    ...formDataAdd,
  })
  if (res.data.code === 0 && res.data.data !== null) {
    message.success('添加成功')
    fetchData()
    addModel.value = false
  } else {
    message.error('添加失败, ' + res.data.message)
  }
}

const showAddModel = () => {
  formDataAdd.teacherId = undefined
  formDataAdd.teacherName = ''
  formDataAdd.classId = undefined
  formDataAdd.className = ''
  formDataAdd.subject = ''
  formDataAdd.weekday = ''
  formDataAdd.classroom = ''
  formDataAdd.lessonPeriod = ''
  formDataAdd.term = ''
  addModel.value = true
}

const showUpdateModel = async (id: number) => {
  const res = await getTeachingScheduleByIdUsingGet({ id })
  if (res.data.code === 0 && res.data.data !== null) {
    formDataUpdate.value = {
      ...res.data.data,
    }
    updateModel.value = true
  } else {
    message.error('获取数据失败, ' + res.data.message)
  }
}
</script>

<style scoped></style>
